package com.kjj;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Product {
    String id;
    String link = "http://www.baixing.com";
    Bitmap[] pictures = new Bitmap[3];
    String[] picurl = new String[3];
    String price;
    String description;

    public Product() {
	this.price = "";
	this.description = "";
    }

    public Product(Bitmap[] pictures, String[] picurl, String price,
	    String description) {
	this.pictures = pictures;
	this.picurl = picurl;
	this.price = price;
	this.description = description;
    }

    public boolean save(KjjActivity activity, int index) {
	try {
	    Properties prop = new Properties();
	    prop.put("price", price);
	    prop.put("description", description);
	    FileOutputStream stream = activity.openFileOutput("product" + index
		    + ".cfg", Context.MODE_PRIVATE);
	    prop.store(stream, "");
	    stream = activity.openFileOutput("product" + index + ".img",
		    Context.MODE_PRIVATE);
	    for (int i = 0; i < pictures.length; i++) {
		if (pictures[i] != null) {
		    pictures[i]
			    .compress(Bitmap.CompressFormat.PNG, 100, stream);
		    break;
		}
	    }
	    stream.flush();
	    stream.close();
	} catch (Exception ex) {
	    ex.printStackTrace();
	}
	return true;
    }

    public boolean load(KjjActivity activity, int index) {
	try {
	    Properties prop = new Properties();
	    FileInputStream stream = activity.openFileInput("product" + index
		    + ".cfg");
	    prop.load(stream);
	    price = prop.get("price").toString();
	    description = prop.get("description").toString();
	    stream = activity.openFileInput("product" + index + ".img");
	    pictures[0] = BitmapFactory.decodeStream(stream);
	} catch (Exception ex) {
	    ex.printStackTrace();
	}
	return true;
    }
}
